package com.solvePlat.config;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.pool.SimpleChannelPool;
import io.netty.handler.codec.http.FullHttpRequest;

import java.io.File;
import java.net.URL;

public class HttpRequestHandler extends SimpleChannelInboundHandler {

    private final String chartUri;
    private static File indexFile = null;
    static {
        try{
            URL location = HttpRequestHandler.class.getProtectionDomain().getCodeSource().getLocation();
            String path = location.toURI().getPath()+"index.html";
            indexFile = new File(path);
            System.out.println(indexFile.toPath());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public HttpRequestHandler(String chatUri){
        this.chartUri = chatUri;
    }
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object o) throws Exception {
        FullHttpRequest fullHttpRequest = (FullHttpRequest) o;
        if (chartUri.equalsIgnoreCase(fullHttpRequest.uri())){
            System.out.println("请求的是websocket请求");
             ctx.fireChannelRead(fullHttpRequest.retain());//如果是websokcet请求转到下一个工序,进行处理
        }else{
            System.out.println("请求的是读取index.html请求");
        }
    }
}
